#include "Node.h"
#include "FEMModel.h"

void NodesBoundingSphere (FiniteElementModel*fem, bool d,
							Vector3 *center, Real * radius)
{
	*center = Vector3 (0,0,0);

	for (int i = fem->NodeCount ();
		i--;)
	{
		*center += fem->GetNode (i)->Location (d);
	}

	*center *=  1.0 / fem->NodeCount();
	*radius =0.0;  

	for (int i = fem->NodeCount ();
		i--;)
	{
		Real r = (fem->GetNode (i)->Location (d) - *center).Length();
		*radius = *radius >r? *radius:r;
	}
}


void NodesBoundingBox (FiniteElementModel*fem, bool d,
						 Vector3 *min, Vector3 * max)
{
	const Real inf = 1e8;
	for (int i=3; i--; )
	{
		(*min)(i) = inf;
		(*max)(i) = -inf;
	}

	for (int j = fem->NodeCount ();
		j--;)
	{
		Vector3 l (fem->GetNode (j)->Location (d));

		for (int i=3; i--; )
		{
			(*min)(i) = (*min)(i) <l(i)? (*min)(i): l(i);
			(*max)(i) = (*max)(i) >l(i)? (*max)(i): l(i);
		}
	}
}
